package com.deer.wms.system.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.deer.wms.busine.tactic.model.Upper.UpperTactic;
import com.deer.wms.busine.tactic.service.UpperTacticService;
import com.deer.wms.intercept.common.data.CurrentUser;
import com.deer.wms.project.root.core.result.CommonCode;
import com.deer.wms.project.root.core.result.Result;
import com.deer.wms.project.root.core.result.ResultGenerator;
import com.deer.wms.project.root.exception.ServiceException;
import com.deer.wms.project.root.service.SuperServiceImpl;
import com.deer.wms.project.root.util.DateUtils;
import com.deer.wms.project.root.util.RedisUtil;
import com.deer.wms.project.root.util.StringUtils;
import com.deer.wms.system.manage.dao.OrganizationMapper;
import com.deer.wms.system.manage.model.organization.Organization;
import com.deer.wms.system.manage.model.organization.OrganizationCriteria;
import com.deer.wms.system.manage.model.organization.OrganizationDto;
import com.deer.wms.system.manage.model.organization.OrganizationTactic;
import com.deer.wms.system.manage.service.OrganizationService;
import com.deer.wms.system.manage.service.OrganizationTacticService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by guo on 2019/11/05.
 */
@Service
@Transactional
public class OrganizationServiceImpl extends SuperServiceImpl<OrganizationMapper, Organization> implements OrganizationService {

    @Autowired
    private OrganizationMapper organizationMapper;

    @Autowired
    private OrganizationService organizationService;

    @Autowired
    private UpperTacticService upperTacticService;

    @Autowired
    private OrganizationTacticService organizationTacticService;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public List<OrganizationDto> findList(OrganizationCriteria criteria) {
        return organizationMapper.findList(criteria);
    }

    @Override
    public List<OrganizationDto> findByOrganizationCode(String organizationCode) {
        return organizationMapper.findByOrganizationCode(organizationCode);
    }

    @Override
    public List<OrganizationDto> findByOrganizationName(String organizationName) {
        return organizationMapper.findByOrganizationName(organizationName);
    }

    @Override
    public void deleteByCodeAndCom(OrganizationCriteria criteria) {
        organizationMapper.deleteByCodeAndCom(criteria);
    }

    @Override
    public Result createOrganization(OrganizationDto organization, CurrentUser currentUser) {
        if (currentUser == null) {
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER, null);
        }
        //校验是否已存在
        List<OrganizationDto> list = organizationService.findByOrganizationCode(organization.getOrganizationCode());
        List<OrganizationDto> list1 = organizationService.findByOrganizationName(organization.getOrganizationName());

        if (list.size() == 0 && list1.size() == 0) {
            String nowDate = DateUtils.getNowDateTimeString();
            organization.setCreateTime(nowDate);
            save(organization);
            if (StringUtils.isNotEmpty(organization.getUpperTacticCode())) {
                List<UpperTactic> list2 = upperTacticService.findByUpperTacticCode(organization.getUpperTacticCode());
                OrganizationTactic organizationTactic = new OrganizationTactic();
                int size = list2.size();
                for (int i = 0; i <= size; i++) {
                    organizationTactic.setOrganizationId(organization.getOrganizationId());
                    organizationTactic.setUpperTacticCode(list2.get(i).getUpperTacticCode());
                    organizationTactic.setUpperTacticName(list2.get(i).getUpperTacticName());
                    organizationTactic.setWareId(list2.get(i).getWareId());
                    organizationTacticService.save(organizationTactic);
                }
            }
            return ResultGenerator.genSuccessResult();
        } else if (list.size() == 0) {
            throw new ServiceException(CommonCode.ORGANIZATION_EXISTS, "此货主已存在");
        } else {
            throw new ServiceException(CommonCode.ORGANIZATION_CODE_EXISTS, "此货主编码已存在");

        }
    }


    @Override
    public void modifyUserInfoAccountState(Integer organizationId, String sate, CurrentUser currentUser) {
        Organization organization = new Organization();
        organization.setOrganizationId(organizationId);
        organization.setOrganizationStatus(sate);
        super.updateById(organization);
    }

    @Override
    public Organization findBy(String organizationCode, String orgCode) {
        QueryWrapper<Organization> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(organizationCode, orgCode);
        return this.getOne(queryWrapper);
    }

    @Override
    public boolean save(Organization organization) {
        super.save(organization);
        redisUtil.remove("allOrganization");
        return true;
    }

    public void deleteById(Integer organizationId) {
        super.removeById(organizationId);
        redisUtil.remove("allOrganization");
    }

    public void update(Organization organization) {
        super.updateById(organization);
        redisUtil.remove("allOrganization");
    }


}
